<?php

/**
 * This is the model class for table "article".
 *
 * The followings are the available columns in table 'article':
 * @property integer $id
 * @property string $title
 * @property string $summary
 * @property string $content
 * @property integer $is_public
 * @property string $cover_image
 * @property integer $website_id
 * @property integer $category_id
 * @property string $created_at
 *
 * The followings are the available model relations:
 * @property Website $website
 * @property Category $category
 * @property Comment[] $comments
 * @property LikeArticle[] $likeArticles
 * @property UserArticle[] $userArticles
 */
class Article extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Article the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'article';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('category_id, created_at', 'required'),
			array('is_public, website_id, category_id', 'numerical', 'integerOnly'=>true),
			array('cover_image', 'length', 'max'=>256),
			array('title, summary, content', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, title, summary, content, is_public, cover_image, website_id, category_id, created_at', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'website' => array(self::BELONGS_TO, 'Website', 'website_id'),
			'category' => array(self::BELONGS_TO, 'Category', 'category_id'),
			'comments' => array(self::HAS_MANY, 'Comment', 'article_id'),
			'likeArticles' => array(self::HAS_MANY, 'LikeArticle', 'article_id'),
			'userArticles' => array(self::HAS_MANY, 'UserArticle', 'article_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => 'Title',
			'summary' => 'Summary',
			'content' => 'Content',
			'is_public' => 'Is Public',
			'cover_image' => 'Cover Image',
			'website_id' => 'Website',
			'category_id' => 'Category',
			'created_at' => 'Created At',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('summary',$this->summary,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('is_public',$this->is_public);
		$criteria->compare('cover_image',$this->cover_image,true);
		$criteria->compare('website_id',$this->website_id);
		$criteria->compare('category_id',$this->category_id);
		$criteria->compare('created_at',$this->created_at,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}